<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/29 0029
 * Time: 下午 1:31
 */
namespace Company\Controller;
use Think\Controller;

class SiteController extends Controller
{

    public function getSite()
    {
        //  获取数据库网站信息
        $company = $_SESSION['user'];

        $site=D("site");

        $list=$site->where(['cid'=>$company])->select();

        if (!empty($list)){
            $this->assign("list",$list);
            //传输皮肤图片
            if ($list[0]['skin'] == 'Default'){
                $this->assign("skin","/images/default.png");
                $this->display();
            }
            else{
                $this->assign("skin","/images/blue.png");
                $this->display();
            }

        }
        else{
            $this->error('您可以先创建一个网站','/site_add.html');
        }

    }

    public function addSite(){
        //创建网站
        $data['cid'] = $_SESSION['user'];
        $data['name'] = $_POST['name'];
        $data['skin'] = $_POST['skin'];
        $data['customs'] = $_POST['customs'];
        $data['title'] = $_POST['title'];
        $data['domain'] = $_POST['domain'];

        $site=D("site");
        if ($data['name'] != null && $data['skin']!=null && $data['customs']!=null && $data['domain']!=null){
            $list=$site->where(['cid'=>$data['cid']])->select();

            if (empty($list)){
                $re=$site->add($data);

                if(!empty($re)){
                    $this->success('创建成功','getSite');
                }
                else{
                    $this->error('创建失败','/site_add.html');
                }
            }
            else{
                $this->error('该用户已创建过一个网站，请不要重复操作','getSite');
            }
        }
        else{
            $this->error('请填完选项！','/site_add.html');
        }


    }
    public function updateSite(){
        //传输网站参数，申请网站更改
        $company = $_SESSION['user'];

        $site=D("site");

        $list=$site->where(['cid'=>$company])->select();

        if (!empty($list)){
            $this->assign("list",$list);
            //传输皮肤图片
            if ($list[0]['skin'] == 'Default'){
                $this->assign("skin",["name"=>"Default","img"=>"/images/default.png"]);
                $this->assign("skin2",["name"=>"Blue","img"=>"/images/blue.png"]);
                $this->display();
            }
            else{
                $this->assign("skin",["name"=>"Blue","img"=>"/images/blue.png"]);
                $this->assign("skin2",["name"=>"Default","img"=>"/images/default.png"]);
                $this->display();
            }

        }
        else{
            $this->error('您可以先创建一个网站','/site_add.html');
        }
    }

    public function upSite(){
        //修改网站参数
        $cid = $_SESSION['user'];
        $data['name'] = $_POST['name'];
        $data['skin'] = $_POST['skin'];
        $data['customs'] = $_POST['customs'];
        $data['title'] = $_POST['title'];
        $data['domain'] = $_POST['domain'];

        $site = D("site");

        $re=$site->where("cid='$cid'")->save($data);

        if(!empty($re)){
            $this->success('更改成功','getSite');
        }
        else{
            $this->error('更改失败','getSite');
        }

    }
}